home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 201-220 / scopedisk215 / listcom / listcom.c < prev    next >
C/C++ Source or Header  |  1995-03-19  |  13KB  |  378 lines

  1. ;/*
  2. ; *************************************************************************
  3. ; * List Comment                   Created: August 11, 1991               *
  4. ; * ©1991  John Bianchi            Bianchi Computer Systems               *
  5. ; *************************************************************************
  6. ; *              Execute me to compile under SAS/C 5.10a                  *
  7. ; *************************************************************************
  8.  
  9. if NOT exists PatMatch.o
  10.    lc -tr PatMatch.c
  11. endif
  12. lc -tr ListCom.c 
  13. blink FROM LIB:c.o+"ListCom.o"+"PatMatch.o" TO "ListCom" LIB LIB:lc.lib LIB:amiga.lib
  14. quit
  15.  
  16. */
  17.  
  18. #include <libraries/dos.h>
  19. #include <libraries/dosextens.h>
  20. #include <exec/types.h>
  21. #include <exec/memory.h>
  22. #include <string.h>
  23.  
  24. #define PROGRAM    "ListCom" 
  25. #define VERS       "1.33"
  26. #define RELEASE    "18.10.1991"
  27. #define COPYRIGHT  "©1991 John Bianchi  All Rights Reserved." 
  28.  
  29. /* 2.0 compatable Version Tag String                                    */
  30. UBYTE *version      = "\0$VER: List Comments " VERS "   (17.10.91) ";
  31.  
  32. UBYTE *Usage        = "\
  33. LIST COMMENTS   Version: " VERS "   " COPYRIGHT "\n\
  34. \n\
  35. Usage:\n   " PROGRAM "  <Path> <Comment> <-options>\n\
  36. \n\
  37. With:\n\
  38.    Path    : the path and/or file(s) you want to list\n\
  39.    Comment : the comment(s) you want to match\n\n\
  40. Options:\n\
  41.    -M = Match  : stop on first found match\n\
  42.    -F = NoForm : don't format the output listing\n\
  43.    -H = NoHead : do not show header line\n\
  44.    -S = NoSize : don't show file size(s)\n\
  45.    -C = NoCom  : don't show file comment(s)\n\
  46.    -Q = Quiet  : (-H -S -C -F) only show the file name(s)\n\
  47.    -N = NoLine : don't do a line feed (-m)\n\
  48. \n";
  49.  
  50. struct FileInfoBlock *f_info;
  51.  
  52. int  ListCom();          /* Parses and does directory type stuff         */
  53. int  showfileinfo();     /* shows a file info block in special format(s) */
  54. BOOL isapat();           /* checks for pattern characters                */
  55. BOOL patternmatch();     /* calls PatMatch.c funcs, returns TRUE/FALSE   */
  56.  
  57.  
  58. char err_msg[80];        /* My error messages                            */
  59. int  return_value;       /* depends upon return of ListCom() routine     */
  60. char Pat[128];           /* here for PatMatch.c stuff written/translated */
  61. char Str[128];           /* by Jeff Lydiatt                              */
  62. WORD Aux[128];           /* See  PatMatch.c  source for more info        */
  63.  
  64. /* flags for the argument switches                                       */
  65. BOOL match,nohead,noform,nosize,nocom,noline = FALSE;
  66.  
  67. main (argc, argv)
  68. int argc;
  69. char *argv[];
  70. {
  71.     char   filepath[255], filenote[80];
  72.     int    k,error;
  73.     struct Process *proc;
  74.     struct CommandLineInterface *cli;
  75.  
  76.     proc = (struct Process *)FindTask(NULL);
  77.     cli =  (struct CommandLineInterface *)(proc->pr_CLI << 2);
  78.     if(!cli  || argc == 0) exit(RETURN_ERROR);
  79.  
  80.     if((f_info=(struct FileInfoBlock *)
  81.        AllocMem(sizeof(struct FileInfoBlock),MEMF_CHIP|MEMF_CLEAR))==0)
  82.     {
  83.         printf("%s  Can't Allocate Memory!",argv[0]);
  84.         exit(RETURN_FAIL);
  85.     }
  86.  
  87.         /* if no arguments supplied or the user type ? show usage       */
  88.         /* Will probably use the 2.0 template stuff after 2.0           */
  89.         /* is the OS of choice. (and i see some clean, small source     */
  90.         /* code that I can learn it from!                               */
  91.  
  92.     if(argv[1][0]=='?') 
  93.        {
  94.         printf("%s",Usage);
  95.         exit(RETURN_WARN);
  96.     }
  97.  
  98.     /* if not given, we must initialize */
  99.     filepath[0] = '\0';
  100.     filenote[0] = '\0';
  101.  
  102.     /* This is the Argument parsing stuff */
  103.     for(k=1; k<argc; k++)
  104.     {
  105.         /* this is to find the '-' commands */
  106.         if(argv[k][0] == '-')
  107.         {
  108.             switch( argv[k][1] )
  109.             {
  110.                 case 'N':
  111.                 case 'n':
  112.                     noline=TRUE;
  113.                 case 'm':
  114.                 case 'M':
  115.                     match=TRUE;
  116.                     break;
  117.                 case 'Q':
  118.                 case 'q':
  119.                     nohead=TRUE;
  120.                     nosize=TRUE;
  121.                     nocom=TRUE;
  122.                 case 'f':
  123.                 case 'F':
  124.                     noform=TRUE;
  125.                     break;
  126.                 case 'H':
  127.                 case 'h':
  128.                     nohead=TRUE;
  129.                     break;
  130.                 case 'S':
  131.                 case 's':
  132.                     nosize=TRUE;
  133.                     break;
  134.                 case 'C':
  135.                 case 'c':
  136.                     nocom=TRUE;
  137.                 case '\0'             /* so a '-' alone will be ignored */
  138.                     break;            /* ignore the compiler warning    */
  139.                 default:
  140.                     printf("\nUnknown command: %s\n",argv[k]);
  141.                     exit(RETURN_ERROR);
  142.              }
  143.         }
  144.         else
  145.         {
  146.             /* this puts the 1st and 2nd argument into their variables  */
  147.             switch( k )     
  148.             {
  149.                 case 1:
  150.                     strcpy(filepath, argv[k]);
  151.                     break;
  152.                 case 2:
  153.                     strcpy(filenote, argv[k]);
  154.                     break;
  155.                 default:
  156.                     printf("\nUnknown command: (%d) \"%s\"\n",k,argv[k]);
  157.                     exit(RETURN_ERROR);
  158.             }
  159.         }
  160.     }
  161.     /********************************************************************/
  162.     /* ok, here we send the path and note to be searched.               */
  163.     /* ListCom() should definately be cleaned up, but this part of the  */
  164.     /* code will remain the same.  ListCom() returns an int value of the*/
  165.     /* error encountered.  err_msg[] will have the error message for    */
  166.     /* a returned value of -1.                                          */
  167.     /********************************************************************/
  168.     switch( error = ListCom(filepath,filenote) )       
  169.     {
  170.         case  0:
  171.             return_value = RETURN_OK;       /* listed something          */
  172.             break;
  173.         case  1:
  174.             return_value = RETURN_WARN;     /* didn't match included for */ 
  175.             break;                          /* script usage              */
  176.         case -1:
  177.             return_value = RETURN_ERROR;    /* some kinda error          */
  178.             printf("ListCom Error: %s.\n",err_msg);
  179.             break;
  180.         default:
  181.             return_value = error;           /* AimgaDOS error            */
  182.             printf("AmigaDOS Error: %d\n",error);
  183.             break;
  184.     }
  185.  
  186.     FreeMem(f_info,sizeof(struct FileInfoBlock));
  187.     exit(return_value);
  188. }
  189.  
  190. int ListCom(filepath, filenote)
  191. char *filepath,*filenote;
  192. {
  193.     int  i,error = 0;
  194.     int  showd = 1;
  195.     long lock;
  196.     char patternpath[80],patternfile[80];
  197.     BOOL name_patmatch=FALSE;
  198.  
  199.     lock = Lock(filepath, ACCESS_READ);
  200.                  /* have to do this as I don't know any other way! */
  201.     if(!lock)    if(isapat(filepath))
  202.     {
  203.         strcpy(patternpath,filepath);
  204.         /* patternpath is a copy of filepath, let's remove the file
  205.            portion                                                      */
  206.         for(i=(strlen(patternpath));i>=0;i--)
  207.         {
  208.             if(patternpath[i] == ':' || patternpath[i] == '/')
  209.                 break;
  210.             else
  211.                 patternpath[i] = '\0';
  212.         }
  213.  
  214.         /* put the patterned file info into patternfile                 */
  215.         /* if any path exists from above, pull filename part            */
  216.         if(strlen(patternpath)>0)
  217.         {
  218.             error=strmid(filepath,patternfile,i+2,30);
  219.             if(error)     /* not sure why this would come up, but...    */
  220.             {
  221.                 strcpy(err_msg,"Error in internal string manipulation");
  222.                 return(-1);
  223.             }
  224.             /* We're only here to pattern match the last node of the
  225.                filepath.  ie: DH0:#?/test  <- Not allowed! (Maybe I'll
  226.                get that in a later version when I learn more)           */
  227.             if(isapat(patternpath))
  228.             {
  229.                 strcpy(err_msg,"Can't pattern match the Path!");
  230.                 return(-1);
  231.             }
  232.         }
  233.         else
  234.             strcpy(patternfile,filepath);
  235.  
  236.         /* patternpath is really the path part of the argument, it
  237.            will not use any wildcards, only "" for current dir.         */
  238.         lock=Lock(patternpath, ACCESS_READ);
  239.         if(!lock)
  240.         {
  241.             strcpy(err_msg,"`");
  242.             strcat(err_msg,patternpath);
  243.             strcat(err_msg,"' path not found");
  244.             return(-1);
  245.         }
  246.         name_patmatch=TRUE;
  247.     }
  248.     else    /* failed orig lock and did not have any wildcards          */
  249.     {
  250.         strcpy(err_msg,"`");
  251.         strcat(err_msg,filepath);
  252.         strcat(err_msg,"' file not found");
  253.         return(-1);
  254.     }
  255.  
  256.     if((Examine(lock,f_info))==0)
  257.     {
  258.         strcpy(err_msg,"Unknown File");  /* this should never happen    */
  259.         return(-1);
  260.     }
  261.     if(f_info->fib_DirEntryType > 0)   /* original lock is a Directory!! */
  262.     {
  263.         if(!nohead)
  264.             printf("Listing of directory: \"%s\"\n",f_info->fib_FileName);   
  265.  
  266.         /*  Loop to read entries in directory                           */
  267.         while ((ExNext(lock,f_info))!=0)
  268.         {    
  269.             /*  if were listing the whole directory or parrern matching
  270.                 a filename                                              */
  271.             if(!name_patmatch || patternmatch(patternfile,f_info->fib_FileName))
  272.             {
  273.                 /* is there a filenote to check? */
  274.                 if(strlen(filenote)>0)   
  275.                 {
  276.                     /*  yes, so see if it is a pattern match to the
  277.                         current file entry's comment                    */
  278.                     if(patternmatch(filenote,f_info->fib_Comment)>0)
  279.                     {
  280.                         showd=showfileinfo();
  281.                         if(match)   break;
  282.                     }
  283.                 }
  284.                 else  /* no filenote, so show all files with comments   */
  285.                     if(strlen(f_info->fib_Comment)>0)
  286.                     {
  287.                         showd=showfileinfo();
  288.                         if(match)   break;
  289.                     }
  290.             }
  291.         }
  292.     }
  293.     else    /* well, we were given a file name, it exists, so show it!  */
  294.     {
  295.         if(!name_patmatch || patternmatch(patternfile,f_info->fib_FileName))
  296.             showd=showfileinfo();
  297.     }
  298.     UnLock(lock);
  299.  
  300.     error = IoErr();
  301.     if(error = ERROR_NO_MORE_ENTRIES)
  302.         return(showd);  /* ignore No more entries and send if we matched */
  303.     else
  304.         return(error);  /* was an AmigaDOS errorx is IoErr()            */
  305. }
  306.  
  307.  
  308. /*  Too much junk to put this in the ListCom() function.                */
  309. /*  This function ALWAYS returns 0 = TRUE.                              */
  310. /*  It is not BOOL due to the nature of the return value which in       */
  311. /*  turn may be used as the return value of ListCom(), this in turn     */
  312. /*  lets main() know how to exit()  ie: RETURN_OK, _WARN, _ERROR, _FAIL */
  313. int showfileinfo(void)
  314. {
  315.     {
  316.         if(noform)
  317.             printf("%s",f_info->fib_FileName);
  318.         else
  319.             printf("%-20s",f_info->fib_FileName);
  320.         if(!nosize)
  321.         {
  322.             if(f_info->fib_DirEntryType > 0 )
  323.                 if(noform)  printf(" Dir");
  324.                 else        printf("     Dir");
  325.             else        
  326.                 if(noform)  printf(" %d",f_info->fib_Size);
  327.             else            printf(" %7ld",f_info->fib_Size);
  328.         }
  329.         if(!nocom)
  330.             if(noform)  printf(" %s",f_info->fib_Comment);
  331.             else        printf(" \"%s\"",f_info->fib_Comment);
  332.     }
  333.     if(!noline)
  334.         printf("\n");
  335.     return(0);
  336. }
  337.  
  338.  
  339. /*  I tried to write this in obscure C (you know, tight, compact,
  340.     un-intelligable)  but couldn't get it to work, so this fatter
  341.     version will have to work till I can rewrite it                */
  342. BOOL isapat(str)
  343. char *str;
  344. {
  345.     int i;
  346.     BOOL flag=FALSE;
  347.     for(i=0; i<strlen(str); i++)
  348.     {
  349.         switch( str[i] )
  350.         {
  351.             case '#': flag=TRUE;
  352.             case '?': flag=TRUE;
  353.             default : break;
  354.         }
  355.     }
  356.     return(flag);
  357. }
  358.  
  359.  
  360. /* this is just a pre-patternmatching function to call              */
  361. /* the functions in PatMatch.c  See PatMatch.c for more             */
  362. BOOL patternmatch(this,that)
  363. char *this,*that;
  364. {
  365.     strcpy(Pat,this);
  366.     strcpy(Str,that);
  367.     if ( CmplPat( Pat, Aux ) == 0 )                    
  368.     {                                               
  369.        strcpy(err_msg,"Bad Wildcard Expression");         
  370.        return(FALSE);                                     
  371.     }                                               
  372.     if ( Match( Pat, Aux, Str ) == 1 )                 
  373.         return(TRUE);                                     
  374.     else                                               
  375.         return(FALSE);                                     
  376. }
  377.  
  378.